package com.wxy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;

/*如果同时定义了mvc和thymeleaf的视图解析器，spring boot通常会先执行thymeleaf的视图解析器。可以通过两种解析器的配置顺序先后自定义优先级。*/

@Configuration
public class springMvcConfig implements WebMvcConfigurer{
	@Override								//前端控制器的配置
	public void configureViewResolvers(ViewResolverRegistry registry) {
		// TODO 自动生成的方法存根
		InternalResourceViewResolver resolver=new InternalResourceViewResolver();
		resolver.setPrefix("/html/");
		resolver.setSuffix(".html");
		resolver.setCache(false);//表示禁用缓存
		registry.viewResolver(resolver);
//		registry.viewResolver(thymeleafViewResolver());
	}

//	@Bean								//通过配置方式添加thymeleaf的视图解析器并添加在mvc视图解析器的后边
//	public ThymeleafViewResolver thymeleafViewResolver() {
//	    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
//	    resolver.setTemplateEngine(templateEngine());
//	    return resolver;
//	}
//
//	@Bean
//	public SpringTemplateEngine templateEngine() {
//	    SpringTemplateEngine engine = new SpringTemplateEngine();
//	    engine.setTemplateResolver(templateResolver());
//	    return engine;
//	}
//
//	@Bean
//	public ITemplateResolver templateResolver() {
//	    SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
//	    resolver.setPrefix("classpath:/static/html/");
//	    resolver.setSuffix(".html");
//	    resolver.setTemplateMode(TemplateMode.HTML);
//	    resolver.setCharacterEncoding("UTF-8");
//	    resolver.setCacheable(false);
//	    return resolver;
//	}
	@Override								//添加拦截器
	public void addInterceptors(InterceptorRegistry registry) {
		// TODO 自动生成的方法存根
		registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/html/hello.html");
	}
}
